const areaList = require('../../areaList/master/areaList');
const index_all = require('../../areaList/master/index_all')
const museum = require('../../areaList/brance/1museum')
const temple = require('../../areaList/brance/2temple')
const geology = require('../../areaList/brance/3geology')
const revolution = require('../../areaList/brance/4revolution')
const greatwall = require('../../areaList/brance/5greatwall')
const grotto = require('../../areaList/brance/6grotto')
const tea = require('../../areaList/brance/7tea')
const glass = require('../../areaList/brance/8glass')
const financial = require('../../areaList/brance/9financial')
const castle = require('../../areaList/brance/10castle')
const image = require('../../areaList/imageRoute/image')
const brance_all = require('../../areaList/brance_all/brance_all')

var myMap

function debounce(func, wait) {
  let timeout
  return function(...args) {
    if (timeout){
      clearTimeout(timeout);
      console.log(111);
    }
    
    timeout = setTimeout(() => {
      func.apply(this, args);
    }, wait);
  };
}

// pages/index/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: image.image.index,
    current:0,
    currentId:0,
    areaList: areaList.areaList,
    longitude: 0.00,
    latitude: 0.00,
    scale: 0.00,
    markers: [],
    polyline: [],
    customCalloutMarkerIds: [],
    mark:[],
    swiperdebounce:null
  },

  bindMap(res) {
    console.log(res.detail);
  },
  //左上角按钮切换路径
  swiper(event) {
    //console.log(event.currentTarget.dataset.item.id);
    const id = event.currentTarget.dataset.item.id
    this.setData({
      current: id
    })
  },

  //轮播图切换
  swiperChange(res) {
    const all = {
      index_all:index_all,
      museum:museum,
      temple:temple,
      geology:geology,
      revolution:revolution,
      greatwall:greatwall,
      grotto:grotto,
      tea:tea,
      glass:glass,
      financial:financial,
      castle:castle
    }
    const currentId = res.detail.current
    const currentName = areaList.areaList[currentId].routeType
    const that = this
    this.setData({
      currentId:currentId,
    })

    this.data.swiperdebounce(that,all,currentId,currentName)
  },

  //路线跳转
  findMore() {
    wx.navigateTo({
      url: "/pages/Pfindmore/pages/findmore/findmore?currentId=" + 0 +  "&areaList_currentId=" + this.data.currentId,
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let mark = []
    for (let i = 0; i < brance_all.name.length; i++) {
      const obj = {
        id:i+1,
        color:brance_all.color[i],
        name:brance_all.name[i],
        remark:brance_all.remark[i]
      }
      mark.push(obj)
    }

    this.setData({
      longitude:areaList.areaList[0].longitude,
      latitude:areaList.areaList[0].latitude,
      scale:areaList.areaList[0].scale,
      
      markers: index_all.index_all.markers,
      customCalloutMarkerIds: index_all.index_all.customCalloutMarkerIds,
      polyline: index_all.index_all.polyline,

      mark:mark
    })

    //防抖函数
    this.setData({
      swiperdebounce:debounce(function(that,all,currentId,currentName){
        that.setData({
          longitude:areaList.areaList[currentId].longitude,
          latitude:areaList.areaList[currentId].latitude,
          scale:areaList.areaList[currentId].scale,
          markers: all[currentName][currentName].markers,
          customCalloutMarkerIds: all[currentName][currentName].customCalloutMarkerIds,
          polyline: all[currentName][currentName].polyline
        })
      },300)
    })
  },
    
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
    myMap = wx.createMapContext('myMap')
    console.log(myMap);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  onShareTimeline: function() {
    // 返回要分享到朋友圈的内容
    return {
      title: '山西文物主题游径小程序',
      query: '', // 页面参数
      imageUrl: 'http://jzroute.oss-cn-hangzhou.aliyuncs.com/enter/enter1.jpg', // 自定义图片路径
    };
  },
})